package com.itheima.attendance.controller;
import com.itheima.attendance.pojo.Attendance;
import com.itheima.attendance.service.AttenArchiveService;
import com.itheima.attendance.service.AttenService;
import com.itheima.attendance.service.CompanySettingsService;
import com.itheima.common.controller.BaseController;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/12 11:12
 */
@CrossOrigin
@RestController
@RequestMapping("/attendances")
public class AttenController extends BaseController {


    @Autowired
    private AttenService attenService;


    @Autowired
    private AttenArchiveService attenArchiveService;


    @Autowired
    private CompanySettingsService companySettingsService;


    /**
     * 上传考勤数据
     */
    @PostMapping("/import")
    public Result importAttenDate(@RequestParam(value = "file") MultipartFile file) throws Exception {
        try{
            attenService.importAttenDate(file,this.companyId);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 查询企业的考勤月列表
     */
    @GetMapping("/findCompanySetttinsList")
    public Result findCompanySetttinsList(){
        return new Result(ResultCode.SUCCESS,attenService.findCompanySetttinsList(this.companyId));
    }

    /**
     * 考勤列表查询
     */
    @GetMapping
    public Result findAttenDateList(@RequestParam("page") Integer page,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("dataMonth") String dataMonth){
        try{
            return new Result(ResultCode.SUCCESS,attenService.findAttenDateList(page,pageSize,dataMonth,this.companyId));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 考勤数据修改
     */
    @PutMapping("/{userid}")
    public Result editAttenDate(@PathVariable("userId") String userId,@RequestBody Attendance attendance){
        try{
            attendance.setUserId(userId);
            attendance.setCompanyId(this.companyId);
            attenService.editAttenDate(attendance);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 查询月考勤归档数据
     */
    @GetMapping("/reports")
    public Result findAttenDateByDateMonth(@RequestParam("archiveDate") String archiveDate){
        try{
            return new Result(ResultCode.SUCCESS,attenArchiveService.findAttenDateByDateMonth(this.companyId,archiveDate));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 月考勤归档数据保存
     */
    @PostMapping("/archive/item")
    public Result item(@RequestParam("archiveDate") String archiveDate) throws Exception {
        try{
            //调用service，实现月考勤数据归档保存
            attenArchiveService.saveArchive(archiveDate,companyId,this.userId);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 制作新的月份报表
     */
    @GetMapping("/newReports")
    public Result newReports(@RequestParam("atteDate") String atteDate,
                             @RequestParam("yearMonth") String yearMonth){
        try{
            companySettingsService.newReports(yearMonth,this.companyId);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 根据年份和企业id查询年度考勤
     */
    @GetMapping("/reports/year")
    public Result findArchiveMonthlyByYearAndCompanyId(@RequestParam("departmentId") String departmentId,
                                                       @RequestParam("year") String year){
        try{
            return new Result(ResultCode.SUCCESS,attenArchiveService.findArchiveMonthlyByYearAndCompanyId(year,this.companyId));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 根据年度考勤的归档id查询考勤归档明细
     */
    @GetMapping("/reports/{id}")
    public Result findArchiveMonthInfoByArchiveId(@PathVariable("id") String id){
        try{
            return new Result(ResultCode.SUCCESS,attenArchiveService.findArchiveMonthInfoByArchiveId(id));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 微服务调用
     * 根据用户id和月份查询已经归档的考勤明细
     */
    @GetMapping("/archive/{userId}/{yearMonth}")
    public Result findArichiveInfoByUserIdAndYearMonth(@PathVariable("userId") String userId,
                                                       @PathVariable("yearMonth") String yearMonth){
        try{
            return new Result(ResultCode.SUCCESS,attenArchiveService.findArichiveInfoByUserIdAndYearMonth(userId,yearMonth));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }
}
